package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.comm.LwClientHelper;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.CdtsRechargeUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.CdtsRechargeUploadResponse;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.IssuerUploadRequest;
import io.renren.common.utils.CreateFileUtil;
import io.renren.common.utils.DateUtil;
import io.renren.common.utils.GUID;
import io.renren.modules.basedata.dao.IssuerUploadDao;
import io.renren.modules.basedata.dao.OperlogDao;
import io.renren.modules.basedata.dao.RechargeUploadDao;
import io.renren.modules.basedata.entity.tbl_Operlog;
import io.renren.modules.basedata.entity.tbl_Recharge;
import io.renren.modules.basedata.entity.tbl_issuer;
import io.renren.modules.basedata.service.IssuerUploadService;
import io.renren.modules.basedata.service.RechargeUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class RechargeUploadServiceImpl implements RechargeUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    RechargeUploadDao rechargeUploadDao;
    @Autowired
    OperlogDao operlogDao;
    @Override
    public String rechargeUpload() throws Exception {


        log.info("开始获取充值信息");

        List<tbl_Recharge> list = null;

        tbl_Recharge recharge = null;

        list = getRechargeList();//获取充值信息

        tbl_Operlog operlog = new tbl_Operlog();

        int operResult=0;

        String FileName = "";

        if(list!=null&&list.size()>0){


            for(int i=0;i<list.size();i++){
                //
                recharge = list.get(i);
//				RechargeUploadRequest request =new RechargeUploadRequest();
                CdtsRechargeUploadRequest request = new CdtsRechargeUploadRequest();
                request.setId(recharge.getId().trim());
                request.setCardId(recharge.getCardId().trim());
                request.setGiftAmount(recharge.getGiftAmount().longValue());
                request.setPaidAmount(recharge.getPaidAmount().longValue());
                request.setRechargeAmount(recharge.getRechargeAmount().longValue());

//				FileName = ETCCommHelper.getFileName(request,"65");
                request.setReqSender("650101");
                //生成就json文件
//				CreateFileUtil.createJsonFile(request.toJsonStr(), "",FileName);

                try{

//				RechargeUploadResponse response = ETCCommHelper.sdtsUpload(FileName, request, RechargeUploadResponse.class);
                    CdtsRechargeUploadResponse response = LwClientHelper.binPostUrl(request);

                    log.info("充值信息返回结果："+response.toJson());

                    if(response!=null&&response.getInfo().equals("成功！")){

                        //上传成功,更新充值信息表状态

                        if(rechargeUploadDao.updateRecharge(recharge.getId())==1){
                            log.info("更新充值数据成功");
                            operResult = 1;
                            operlog = getOperlog(33,1,recharge.getId(),FileName ,operResult, response.getInfo(),"充值信息上传成功,更新成功");
                        }else{
                            log.info("更新充值数据失败");
                            operlog = getOperlog(33,1,recharge.getId(),FileName ,operResult,response.getInfo(),"充值信息上传成功,更新失败");
                            operlogDao.insertlog(operlog);

                            //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                        }
//					sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    }else{
                        log.info("上传充值信息失败,失败原因："+response.getInfo());
                        rechargeUploadDao.updateRechargeError(recharge.getId());
                       // sqlMapClient.update("recharge_ORACLE.updateRechargeError",recharge.getId());

                        //getOperlog（操作类型,传输类型,发送文件名,接收文件名,操作结果,部中心返回结果,说明）
                        operlog = getOperlog(33,1,recharge.getId(),FileName ,0, response.getInfo(),response.toJson());
                        operlogDao.insertlog(operlog);
                        //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    }
                }catch(IOException e){
                    log.info("上传充值信息失败,失败原因："+e.getMessage());

//				sqlMapClient.update("recharge_ORACLE.updateRechargeError",recharge.getId());

                    operlog = getOperlog(33,1,recharge.getId(),FileName ,0, "IO异常","上传信息失败,失败原因：IO异常!"+e.getMessage());
                    operlogDao.insertlog(operlog);
                    //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    e.getStackTrace();
                    continue;
                }catch(ApiRequestException apie){
                    log.info("上传充值信息失败,失败原因："+apie.getMessage());

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        if(apie.getErrCode()==707){
                            rechargeUploadDao.updateRecharge(recharge.getId());
                            //sqlMapClient.update("recharge_ORACLE.updateRecharge",recharge.getId());
                        }else{
                            rechargeUploadDao.updateRechargeError(recharge.getId());
                            //sqlMapClient.update("recharge_ORACLE.updateRechargeError",recharge.getId());
                        }
                    }

                    operlog = getOperlog(33,1,recharge.getId(),FileName ,0, String.valueOf(apie.getErrCode()),"上传信息失败,系统Apie异常:"+apie.getMessage());
                    operlogDao.insertlog(operlog);
                    //sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
                    apie.printStackTrace();
                    continue;
                }
            }
            return "上传充值信息结束!";
        }else{
            return "无充值信息,上传结束...";
        }

    }

    //获取充值信息
    @SuppressWarnings("unchecked")
    public List<tbl_Recharge> getRechargeList() throws Exception {
        List<tbl_Recharge> list;
        try {
            list = rechargeUploadDao.getRechargeList();
            //list = sqlMapClient.queryForList("recharge_ORACLE.getRechargeList");//
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("获取充值信息失败!错误信息：", e1);
            throw new Exception("获取充值信息失败："+e1.getMessage());
        }
        return list;
    }

    //获取operlog信息
    public tbl_Operlog getOperlog(int operType,int softType,String sendFileName,String reFileName,int operResult,String reResult, String content){
        tbl_Operlog operlog = new tbl_Operlog();
        operlog.setWastesn(new GUID().toString());
        operlog.setOperTime(new Date());
        operlog.setOpertype(operType);
        operlog.setSoftType(softType);
        operlog.setSendFileName(sendFileName);
        operlog.setReFileName(reFileName);
        operlog.setOperResult(operResult);
        operlog.setReResult(reResult);
        operlog.setContent(content);
        return operlog;
    }
}
